package com.efounder.view.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.allen.library.SuperTextView;
import com.efounder.mobilemall.R;
import com.utilcode.util.ScreenUtils;
import com.utilcode.util.SizeUtils;

import java.util.List;

/**
 * Created by Marcello on 2018/9/17.
 */
public class MPopuWindow extends PopupWindow implements View.OnClickListener {
    public Context mContext;
    private SuperTextView order_stv_time_dec,order_stv_title;
    private LinearLayout mall_ll_map_view;
    private RadioGroup order_rg_express;
    private EditText order_edt_tel;
    private TextView order_tv_time_picker;
    private int CHECK_TYPE;

    public MPopuWindow(Context context) {
        initView(context);
    }

    private void initView(Context mContext) {
        this.mContext = mContext;
        View v = LayoutInflater.from(mContext).inflate(R.layout.popup_select_express,
                null);
        setContentView(v);

        order_stv_time_dec = (SuperTextView) v.findViewById(R.id.order_stv_time_dec);
        order_stv_title = (SuperTextView) v.findViewById(R.id.order_stv_title);
        mall_ll_map_view = (LinearLayout) v.findViewById(R.id.mall_ll_map_view);
        order_rg_express = (RadioGroup) v.findViewById(R.id.order_rg_express);
        Button order_btn_sure = (Button) v.findViewById(R.id.order_btn_sure);
        order_tv_time_picker = (TextView) v.findViewById(R.id.order_tv_time_picker);
        order_edt_tel = (EditText) v.findViewById(R.id.order_edt_tel);


        order_btn_sure.setOnClickListener(this);
        order_tv_time_picker.setOnClickListener(this);
        if(order_rg_express!=null){
            order_rg_express.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    CHECK_TYPE =checkedId;
                    if(checkedId==0){
                        order_stv_time_dec.setVisibility(View.VISIBLE);
                        mall_ll_map_view.setVisibility(View.GONE);
                    }else {
                        order_stv_time_dec.setVisibility(View.GONE);
                        mall_ll_map_view.setVisibility(View.VISIBLE);
                    }

                }
            });
        }
        order_stv_title.getRightIconIV().setOnClickListener(new View.OnClickListener() {//关闭
            @Override
            public void onClick(View v) {
                if(isShowing()){
                    dismiss();
                }
            }
        });
        order_stv_time_dec.getRightIconIV().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//选择日期
                if(mPopupWindowListener!=null){
                    mPopupWindowListener.selectDate(order_stv_time_dec);
                }
            }
        });
        // 设置PopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置PopupWindow弹出窗体的高
        this.setHeight((int) (ScreenUtils.getScreenHeight()*0.7));

        // 设置PopupWindow弹出窗体可点�?
        this.setTouchable(true);
        this.setFocusable(true);
        this.setOutsideTouchable(true);

        // 刷新状态?
        this.update();
        // 设置cPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.pop_anim);
        // 实例化一个ColorDrawable颜色为白色
        ColorDrawable dw = new ColorDrawable(0xffffff);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
    }

    public void showPopupWindow(View parent) {

        if (!this.isShowing()) {
            this.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        } else {
            this.dismiss();

        }
    }

    @Override
    public void onClick(View arg0) {
        int i = arg0.getId();
        if (i == R.id.order_btn_sure) {
            this.dismiss();
            if(mPopupWindowListener!=null){
                String time="";
                String tel="";
                if(CHECK_TYPE==0){
                     time = order_stv_time_dec.getLeftString().toString().trim();
                }else {
                     time = order_tv_time_picker.getText().toString().trim();
                     tel = order_edt_tel.getText().toString().trim();
                }
                mPopupWindowListener.clickSure(time,tel,CHECK_TYPE);
            }
        }else if (i==R.id.order_tv_time_picker){//选择日期
        if(mPopupWindowListener!=null){
            mPopupWindowListener.selectDate(order_tv_time_picker);
        }
        }
    }
    //动态设置选项
    public MPopuWindow setRGInfo(List<String> express) {
        for (int i = 0; i <express.size() ; i++) {
            RadioButton rb= new RadioButton(mContext);
            RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.WRAP_CONTENT, SizeUtils.dp2px(32));
            layoutParams.setMargins(15, 8, 10, 8);
            rb.setLayoutParams(layoutParams);
            rb.setPadding(15,5,15,5);
            rb.setTextSize(16);
            rb.setGravity(Gravity.CENTER);
            rb.setButtonDrawable(android.R.color.transparent);//隐藏单选圆形按钮
            rb.setBackground(mContext.getResources().getDrawable(R.drawable.select_switch_btn));
            rb.setText(express.get(i));
            rb.setTextColor(mContext.getResources().getColorStateList(R.color.mall_color_yellow));
            if(i==0){
                rb.setChecked(true);
            }else {
                rb.setChecked(false);
            }
            rb.setId(i);
            order_rg_express.addView(rb);
        }
        return this;
    }
    public MPopuWindow setTextViewTime(String time){
        if(order_tv_time_picker!=null){
            order_tv_time_picker.setText(time);
        }
        return this;
    }
    public MPopuWindow setEditTextTel(String tel){
        if(order_edt_tel!=null){
            order_edt_tel.setText(tel);
        }
        return this;
    }
    public MPopuWindow setSurperTextTime(String time){
        if(order_stv_time_dec!=null){
            order_stv_time_dec.setLeftString(time);
        }
        return this;
    }
    public RadioGroup getOrderRgExpress() {
        return order_rg_express;
    }

    public EditText getOrder_edt_tel() {
        return order_edt_tel;
    }
    private MPopupWindowListener mPopupWindowListener;
    public interface MPopupWindowListener{
        void selectDate(TextView textView);
        void selectDate(SuperTextView superTextView);
        void clickSure(String time,String phone,int type);
    }

    public void setPopupWindowListener(MPopupWindowListener mPopupWindowListener) {
        this.mPopupWindowListener = mPopupWindowListener;
    }
}
